/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.repository.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import info.peigen.hotpot.business.account.core.domain.bank.BankConvertor;
import info.peigen.hotpot.business.account.core.repository.entity.BankEntity;
import info.peigen.hotpot.business.account.core.repository.mapper.BankMapper;
import info.peigen.hotpot.common.core.domain.bank.Bank;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.core.util.IdUtil;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import org.noear.solon.annotation.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <b>(BankRepository)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/5
 */
@Component
public class BankRepository extends AbstractBeetlSqlRepository<BankMapper, BankEntity> {
    public String store(Bank bank) {
        if (ObjectUtil.isNull(bank)) {
            return null;
        }

        BankEntity bankEntity = BankConvertor.convertToEntity(bank)
                .setBankId(IdUtil.id())
                .setAddress(JSON.toJSONString(bank.getAddress()))
                .setBank(JSON.toJSONString(bank));

        BankEntity selectEntity = getMapper().findByBankCode(bankEntity.getBankCode());
        // 原币提现银行无BankCode
        if (ObjectUtil.isNull(selectEntity)) {
            selectEntity = getMapper().findBySwiftCode(bankEntity.getSwiftCode());
        }

        if (ObjectUtil.isNotNull(selectEntity)) {
            return selectEntity.getBankId();
        }

        saveOrUpdate(bankEntity);
        return bankEntity.getBankId();
    }

    public void store(Bank bank, BankEntity bankEntity) {
        if (ObjectUtil.isNull(bank) || ObjectUtil.isNull(bankEntity)) {
            return;
        }

        BankEntity selectEntity = getMapper().findByBankCode(bankEntity.getBankCode());
        if (ObjectUtil.isNotNull(selectEntity)) {
            return;
        }

        BankConvertor.convertToEntity(bank, bankEntity);
        saveOrUpdate(bankEntity);
    }

    public Bank active(String bankId) {
        Assert.isTrue(StrUtil.isNotEmpty(bankId), () -> new BusinessException(ResultCode.FAILURE, "激活时[bankId]不得为空!"));

        BankEntity entity = getMapper().findByBankId(bankId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!bankId=" + bankId));

        return BankConvertor.convertToDomain(entity);
    }

    public List<Bank> active(List<String> bankIds) {
        List<BankEntity> entities = getMapper().createLambdaQuery().andIn(BankEntity::getBankId, bankIds).select();
        return entities.stream().map(BankConvertor::convertToEntity).collect(Collectors.toList());
    }
}